package edu.hnu.cloudflight.android.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import edu.hnu.cloudflight.android.R;
import edu.hnu.cloudflight.android.domain.Flight;

public class FlightNumberListAdapter extends BaseAdapter {

	public static final int ShowCity = 0;

	public static final int ShowTime = 1;
	

	private final LayoutInflater mInflater;

	private List<Flight> flights;

	private boolean showCity ;

	public FlightNumberListAdapter(Context context, List<Flight> flights, boolean showCity) {
		this.flights = flights;
		this.showCity=showCity;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		View convertView = view;
		ViewHoder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listview_querybyflightcourse, parent, false);
			holder = new ViewHoder();
			holder.num = (TextView) convertView.findViewById(R.id.query_info_num_show);
			holder.company = (TextView) convertView.findViewById(R.id.query_info_company_show);
			holder.startDate = (TextView) convertView.findViewById(R.id.query_info_starttime_show);
			holder.endDate = (TextView) convertView.findViewById(R.id.query_info_endtime_show);
			holder.firstInfo=(TextView) convertView.findViewById(R.id.query_info_starttime);
			holder.secondInfo=(TextView) convertView.findViewById(R.id.query_info_endtime);
			convertView.setTag(holder);
		} else {
			holder = (ViewHoder) convertView.getTag();
		}
		Flight f = flights.get(position);
		holder.num.setText(f.name);
		holder.company.setText(f.complany);

		if (showCity == true) {
			holder.firstInfo.setText(R.string.flight_start);
			holder.secondInfo.setText(R.string.flight_end);
			holder.startDate.setText(f.start);
			holder.endDate.setText(f.end);
		} else {
			holder.startDate.setText(f.DepTime);
			holder.endDate.setText(f.ArrTime);
		}
		/*
		 * if (position % 2 == 0) {
		 * convertView.setBackgroundResource(R.color.color_list_even); } else {
		 * convertView.setBackgroundResource(R.color.color_list_odd); }
		 */
		return convertView;
	}

	class ViewHoder {
		public TextView num;
		public TextView company;
		public TextView startDate;
		public TextView endDate;
		public TextView firstInfo;
		public TextView secondInfo;
	}

	public void setData(List<Flight> data) {
		if (data != null)
			this.flights = data;
	}

	@Override
	public int getCount() {
		return flights.size();
	}

	@Override
	public Object getItem(int position) {
		return flights.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}